package com.getee.tianchaohongyu.kopier

import java.util.*


/**
 * 节点数据
 * created zpf on 2019/3/11 0011
 * @param root 根节点
 * @param name 对象名称
 */
open class NodeData {

    /**根节点*/
    open lateinit var root: NodeData

    /**父节点*/
    open lateinit var parent: NodeData

    /**名称*/
    open var name: String

    /**格式*/
    open var pattern: String? = null

    /**属性列表*/
    open var properties: MutableList<NodeData> = ArrayList()

    /**获取完整名称*/
    open val fullName: String get() = if (parent == this || parent == root) name else "${parent.name}.${name}"

    /**
     * 构造节点数据
     * @param name 对象名称
     * @param root 根节点
     * @param parent 父节点
     */
    constructor(name: String, root: NodeData? = null, parent: NodeData? = null) {
        this.name = name
        if (root != null) this.root = root
        if (parent != null) this.parent = parent
    }

    override fun toString(): String {
        val builder = StringBuilder(name)
        if (!pattern.isNullOrBlank()) builder.append("(").append(pattern).append(")")
        if (properties.isNotEmpty()) builder.append(properties.toString())
        return builder.toString()
    }
}